package markers;

import common.ChartDemo;
import common.ChartUtil;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryMarker;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.ui.Layer;
import org.jfree.chart.ui.RectangleInsets;
import org.jfree.chart.ui.TextAnchor;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.Dataset;

import java.awt.*;

public class CategoryMarkerDemo1 extends ChartDemo {

    public CategoryMarkerDemo1(String title) {
        super(title);
    }

    @Override
    protected Dataset createDataset() {
        DefaultCategoryDataset defaultCategoryDataset = new DefaultCategoryDataset();
        defaultCategoryDataset.addValue(21D, "Series 1", "Category 1");
        defaultCategoryDataset.addValue(50D, "Series 1", "Category 2");
        defaultCategoryDataset.addValue(152D, "Series 1", "Category 3");
        defaultCategoryDataset.addValue(184D, "Series 1", "Category 4");
        defaultCategoryDataset.addValue(299D, "Series 1", "Category 5");
        return defaultCategoryDataset;
    }

    @Override
    protected JFreeChart createChart(Dataset dataset) {
        CategoryDataset categoryDataset = (CategoryDataset) dataset;
        JFreeChart jFreeChart = ChartFactory.createLineChart("Category Demo 1",
                "Category", "Count", categoryDataset,
                PlotOrientation.VERTICAL, true, true, false);
        CategoryPlot categoryPlot = (CategoryPlot) jFreeChart.getPlot();
        NumberAxis numberAxis = (NumberAxis) categoryPlot.getRangeAxis();
        numberAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

        LineAndShapeRenderer lineAndShapeRenderer = (LineAndShapeRenderer) categoryPlot.getRenderer();
        lineAndShapeRenderer.setSeriesShapesVisible(0, true);
        lineAndShapeRenderer.setDrawOutlines(true);
        lineAndShapeRenderer.setUseFillPaint(true);
        lineAndShapeRenderer.setDefaultFillPaint(Color.white);

        CategoryMarker categoryMarker = new CategoryMarker("Category 4", Color.blue, new BasicStroke(1.0F));
        categoryMarker.setDrawAsLine(true);
        categoryMarker.setLabel("Marker Label");
        categoryMarker.setLabelFont(new Font("Dialog", 0, 11));
        categoryMarker.setLabelTextAnchor(TextAnchor.TOP_RIGHT);
        categoryMarker.setLabelOffset(new RectangleInsets(2D, 5D, 2D, 5D));
        categoryPlot.addDomainMarker(categoryMarker, Layer.BACKGROUND);
        return jFreeChart;
    }

    public static void main(String[] args) {
        CategoryMarkerDemo1 demo1 = new CategoryMarkerDemo1("CategoryMarkerDemo1");
        ChartUtil.showChartDemo(demo1);
    }
}
